package zgrjb.snbq.teach_platform.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zgrjb.snbq.teach_platform.component.MessageWebSocketHandler;
import zgrjb.snbq.teach_platform.component.NotificationWebSocketHandler;
import zgrjb.snbq.teach_platform.util.JsonData;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 健康检查控制器
 * 
 * @author system
 * @since 2024-12-28
 */
@RestController
@RequestMapping("/api/v1/health")
@Slf4j
public class HealthController {

    /**
     * 基础健康检查
     */
    @GetMapping("/check")
    public JsonData healthCheck() {
        Map<String, Object> data = new HashMap<>();
        data.put("status", "UP");
        data.put("timestamp", LocalDateTime.now());
        data.put("message", "XHT教学平台运行正常");
        
        return JsonData.buildSuccess(data);
    }

    /**
     * 详细系统状态
     */
    @GetMapping("/status")
    public JsonData systemStatus() {
        Map<String, Object> data = new HashMap<>();
        
        // 基础信息
        data.put("status", "UP");
        data.put("timestamp", LocalDateTime.now());
        data.put("version", "1.0.0");
        
        // WebSocket连接状态
        Map<String, Object> websocket = new HashMap<>();
        websocket.put("messageConnections", MessageWebSocketHandler.getOnlineUserCount());
        websocket.put("notificationConnections", NotificationWebSocketHandler.getOnlineUserCount());
        data.put("websocket", websocket);
        
        // 系统信息
        Map<String, Object> system = new HashMap<>();
        system.put("javaVersion", System.getProperty("java.version"));
        system.put("osName", System.getProperty("os.name"));
        system.put("osVersion", System.getProperty("os.version"));
        system.put("maxMemory", Runtime.getRuntime().maxMemory() / 1024 / 1024 + "MB");
        system.put("totalMemory", Runtime.getRuntime().totalMemory() / 1024 / 1024 + "MB");
        system.put("freeMemory", Runtime.getRuntime().freeMemory() / 1024 / 1024 + "MB");
        data.put("system", system);
        
        return JsonData.buildSuccess(data);
    }

    /**
     * 功能模块状态
     */
    @GetMapping("/modules")
    public JsonData moduleStatus() {
        Map<String, Object> data = new HashMap<>();
        
        // 各功能模块状态
        Map<String, String> modules = new HashMap<>();
        modules.put("message", "ACTIVE");
        modules.put("notification", "ACTIVE");
        modules.put("taskQueue", "ACTIVE");
        modules.put("websocket", "ACTIVE");
        modules.put("scheduler", "ACTIVE");
        
        data.put("modules", modules);
        data.put("timestamp", LocalDateTime.now());
        
        return JsonData.buildSuccess(data);
    }
} 